package com.tanhua.manage.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.manage.mapper.UserFreezeMapperXun;
import com.tanhua.manage.pojo.UserFreeze;
import com.tanhua.manage.util.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * xun
 */
@Service
public class UserManageServiceXun {


    @Autowired
    private UserFreezeMapperXun userFreezeMapperXun;


    /**
     * 用户冻结操作
     * @param userId
     * @param freezingTime
     * @param freezingRange
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String userFreeze(Integer userId, Integer freezingTime,Integer freezingRange, String reasonsForFreezing, String frozenRemarks){

        if (freezingTime==1){
            //冻结三天
            if (freezingRange==1){
                //冻结登录
               return this.freezeThreeDayAndLogin(userId,reasonsForFreezing,frozenRemarks);
            }else if (freezingRange==2){
                //冻结发言
               return this.freezeThreeDayAndStatement(userId,reasonsForFreezing,frozenRemarks);
            }else {
                //冻结永久发布状态
               return this.freezeThreeDayAndPerpetualPublish(userId,reasonsForFreezing,frozenRemarks);
            }
        }


        if (freezingTime==2){
            //冻结七天
            if (freezingRange==1){
                //冻结登录
               return this.freezeSevenDayAndLogin(userId,reasonsForFreezing,frozenRemarks);
            }else if (freezingRange==2){
                //冻结发言
               return this.freezeSevenDayAndStatement(userId,reasonsForFreezing,frozenRemarks);
            }else {
                //冻结永久发布状态
               return this.freezeSevenDayAndPerpetualPublish(userId,reasonsForFreezing,frozenRemarks);
            }
        }


        if (freezingTime==3){
            //永久冻结
            if (freezingRange==1){
                //冻结登录
               return this.freezePerpetualAndLogin(userId,reasonsForFreezing,frozenRemarks);
            }else if (freezingRange==2){
                //冻结发言
               return this.freezePerpetualAndStatement(userId,reasonsForFreezing,frozenRemarks);
            }else {
                //冻结永久发状态
               return this.freezePerpetualAndPerpetualPublish(userId,reasonsForFreezing,frozenRemarks);
            }
        }


         return null;
    }


    /**
     * 把数据存入userFreeze表中
     * @param userId
     * @param freezingTime
     * @param freezingRange
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String fill(Integer userId, Integer freezingTime,Integer freezingRange, String reasonsForFreezing, String frozenRemarks){

        //Admin admin = UserThreadLocal.get();

        //创建冻结对象
        UserFreeze userFreeze=new UserFreeze();

        Date date=new Date();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            sdf.format(date);
            userFreeze.setCreated(date);
            userFreeze.setUpdated(date);
            userFreeze.setUserId(userId);
            userFreeze.setFreezingTime(freezingTime);
            userFreeze.setFreezingRange(freezingRange);
            userFreeze.setReasonsForFreezing(reasonsForFreezing);
            userFreeze.setFrozenRemarks(frozenRemarks);

            //查询冻结表中是否有该id,防止绕过前端界面,直接访问后台
            QueryWrapper<UserFreeze> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);

            //如果冻结表中没有该userId,则冻结该用户
            if (this.userFreezeMapperXun.selectOne(queryWrapper)==null){
                this.userFreezeMapperXun.insert(userFreeze);
                return "操作成功";
            }

            return "操作失败";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "操作失败";

    }






    /**
     * 冻结三天,冻结登录
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezeThreeDayAndLogin(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,1,1,reasonsForFreezing,frozenRemarks);
    }



    /**
     * 冻结三天,冻结发言
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezeThreeDayAndStatement(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,1,2,reasonsForFreezing,frozenRemarks);
    }


    /**
     * 冻结三天,冻结永久发布状态
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezeThreeDayAndPerpetualPublish(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,1,3,reasonsForFreezing,frozenRemarks);
    }



    /**
     * 冻结七天,冻结登录
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezeSevenDayAndLogin(Integer userId,String reasonsForFreezing,String frozenRemarks){
         return this.fill(userId,2,1,reasonsForFreezing,frozenRemarks);
    }


    /**
     * 冻结七天,冻结发言
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezeSevenDayAndStatement(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,2,2,reasonsForFreezing,frozenRemarks);
    }


    /**
     * 冻结七天,冻结永久发布状态
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezeSevenDayAndPerpetualPublish(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,2,3,reasonsForFreezing,frozenRemarks);
    }


    /**
     * 永久冻结,冻结登录
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezePerpetualAndLogin(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,3,1,reasonsForFreezing,frozenRemarks);
    }


    /**
     * 永久冻结,冻结发言
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezePerpetualAndStatement(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,3,2,reasonsForFreezing,frozenRemarks);
    }


    /**
     * 永久冻结,冻结永久发布状态
     * @param userId
     * @param reasonsForFreezing
     * @param frozenRemarks
     * @return
     */
    public String freezePerpetualAndPerpetualPublish(Integer userId,String reasonsForFreezing,String frozenRemarks){
        return this.fill(userId,3,3,reasonsForFreezing,frozenRemarks);
    }






    /**
     * 用户解冻操作
     * @param userId
     * @param reasonsForThawing
     * @return
     */
    public String userUnFreeze(Integer userId, String reasonsForThawing) {
        try {
            QueryWrapper<UserFreeze> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            this.userFreezeMapperXun.delete(queryWrapper);
            return "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "操作失败";
    }
}
